package com.blt.car.utils;

import android.content.Context;
import android.content.DialogInterface;

import com.blt.car.CarApp;
import com.blt.car.widget.dialog.LoadingDialog;

import java.lang.ref.WeakReference;

import blt.app_core.log.Logger;

/**
 * Created by xgp on 2017/6/16.
 */

public class Uiutils {
    private static WeakReference<LoadingDialog> mProgressDialogRef;


    /**
     * Loading弹框
     */
    public static void progressShow(Context ctx) {
        progressShow(null, ctx, null);
    }

    /**
     * Loading弹框
     *
     * @param msg
     */
    public static void progressShow(Context ctx, String msg) {
        progressShow(msg, ctx, null);
    }

    /**
     * Loading弹框
     *
     * @param msg
     * @param dismissListener
     */
    public static void progressShow(String msg, Context ctx, DialogInterface.OnDismissListener dismissListener) {
        progressCancel();
        LoadingDialog progressDialog = new LoadingDialog(ctx, msg);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setOnDismissListener(dismissListener);
        progressDialog.show();
        mProgressDialogRef = new WeakReference<>(progressDialog);
    }

    /**
     * 取消等待弹框
     */
    public static void progressCancel() {
        try {
            if (mProgressDialogRef != null && mProgressDialogRef.get() != null && mProgressDialogRef.get().isShowing()) {
                mProgressDialogRef.get().dismiss();
            }
        } catch (final Exception e) {
            Logger.e("progressCancel Exception" + e);
        } finally {
            mProgressDialogRef = null;
        }
    }

    public static int dip2px(float dpValue) {
        Context context = CarApp.getContext();
        final float scale = context.getResources()
                .getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
